import { defineStore } from "pinia";
import config from "@/config";
import { modal } from "@/plugins"
import {
	env,
	sysProperties,
	allProvince,
	allRegion,
	navigationName,
	navigationLogo } from '@/api/system/common'

const useSetStore = defineStore("set", {
  state: () => ({
    baseUrl: config.baseUrl,
		timer: null,
		sys: {},
		menuButton: {
			height: 32,
			top: 0,
			left: 260
		},
		provinceList: [],
		region: [],
		cacheGoods:[],
		appLogo: '',
		appName: '',
		bindPhone: {},
		spreadCode: '',
		// #ifdef MP-WEIXIN
		indexMode: 2,
		// #endif
		// #ifndef MP-WEIXIN
		indexMode: 2,
		// #endif
  }),
  actions: {
    // 设置服务器地址
    setBaseUrl(url) {
      this.baseUrl = url
      uni.setStorageSync('tyUrl', url)
    },
		// 获取配置信息
		setSys() {
			return new Promise((resolve, reject) => {
			  sysProperties().then((res) => {
					for(let i = 0; i < res.data.length; i++) {
						if (res.data[i].name === 'hot_words') {
							this.sys[res.data[i].name] = res.data[i].value ? res.data[i].value.split('；').filter(item => item) : []
						} else {
							this.sys[res.data[i].name] = res.data[i].value
						}
					}
					resolve(this.sys)
			  }).catch((err) => {
					reject(err)
				})
			})
		},
		// 获取区域信息
		setAllProvince() {
			return new Promise((resolve, reject) => {
			  allProvince().then((res) => {
					this.provinceList = res.data.map(item => {
						return {
							...item,
							value: item.code,
							label: item.name
						}
					}) || []
					resolve(res.data)
			  }).catch((err) => {
					reject(err)
				})
			})
		},
		// 获取省市区
		setRegion() {
			return new Promise((resolve, reject) => {
			  allRegion().then((res) => {
					this.region = res.data
					resolve(res.data)
			  }).catch((err) => {
					reject(err)
				})
			})
		},
		// 设置定时任务
		setTimer(userId) {
			if (!this.timer && userId && this.openTimer) {
				console.log('开始定时')
				addUserLocation(userId)
				this.timer = setInterval(() => {
					addUserLocation(userId)
				}, licationTime)
			}
		},
		clearTimer() {
			// console.log('清空定时')
			clearInterval(this.timer)
			this.timer = null
		},
		// 获取微信小程序胶囊信息
		getMenuButton() {
			return new Promise((resolve, reject) => {
			  let height = 32
			  let top = 0
				let barHeight = 0
			  // #ifdef MP-WEIXIN
			  let left = 260
			  const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			  if (menuButtonInfo) {
			  	height = menuButtonInfo.height
			  	top = menuButtonInfo.top
			  	left = menuButtonInfo.left
			  }
			  // #endif
			  // #ifndef MP-WEIXIN
			  let left = uni.getWindowInfo().windowWidth
				barHeight = uni.getWindowInfo().statusBarHeight
			  // #endif
			  this.menuButton =  {
			  	height,
			  	top,
			  	left,
					barHeight
			  }
				resolve({
			  	height,
			  	top,
			  	left
			  })
			})
		},
		// 获取环境
		getEnv() {
			return new Promise((resolve, reject) => {
			  env().then((res) => {
			  	this.env = res.msg
					resolve(res.msg)
			  }).catch((err) => {
					reject(err)
				})
			})
		},
		// 缓存商品
		setCacheGoods(goods) {
			// console.log(goods)
			if (!goods.categoryId || !goods.regionCode || !goods.sellChannel) return false
			let cacheGoodsFilter = this.cacheGoods.find(item => item.categoryId == goods.categoryId && item.regionCode == goods.regionCode && item.sellChannel == goods.sellChannel)
			if (cacheGoodsFilter) {
				cacheGoodsFilter = {...goods}
			} else {
				this.cacheGoods.push(goods)
			}
		},
		// 获取缓存商品
		getCacheGoods(query) {
			const cacheGoodsFilter = this.cacheGoods.find(item => item.categoryId == query.categoryId && item.regionCode == query.regionCode && item.sellChannel == query.sellChannel)
			// console.log(cacheGoodsFilter)
			return cacheGoodsFilter?.list || null
		},
		// 获取appLogo
		setAppLogo() {
			return new Promise((resolve, reject) => {
			  navigationLogo().then((res) => {
					this.appLogo = res.data
					resolve(res.data)
			  }).catch((err) => {
					reject(err)
				})
			})
		},
		// 获取app名称
		setAppName() {
			return new Promise((resolve, reject) => {
			  navigationName().then((res) => {
					this.appName = res.data
					resolve(res.data)
			  }).catch((err) => {
					reject(err)
				})
			})
		},
		setBindPhone(data) {
			this.bindPhone = {...data}
		}
  },
});

export default useSetStore;
